const {Commodity} = require("../../models/commodity")

module.exports = async (req, res) => {
	var artNo = req.query.Art_No;

  //根据商品信息集合的ISBN查询图书集合中对应ISBN的图书信息的规则
  let opt = [
    {$match:{"Art_No":artNo}},
    { $lookup:{
            from: "books",
            localField: "ISBN",
            foreignField: "ISBN",
            as: "bookInfo" },},
    { $project:{
            "Art_No":1,
            "freight":1,
            "customPrice":1,
            "user_id":1,
            "appearance":1,
            "deliveryLocation":1,
            "bookInfo.ISBN":1,
            "bookInfo.title":1,
            "bookInfo.author":1,
            "bookInfo.img":1,
            "bookInfo.publisher":1,
            "bookInfo.pubdate":1,
            "bookInfo.edition":1,
            "bookInfo.price":1,
            "bookInfo.gist":1,
            "bookInfo.format":1,
            "bookInfo.binding":1,
            "bookInfo.firstCategory":1,
            "bookInfo.secondCategory":1,
        }
    },
    {$skip:0},
    {$limit:10},
]
//根据用户ID联合查询用户信息集合中的信息
let opt2 = [
    {$match:{"Art_No":artNo}},
    { $lookup:{
            from: "users",
            localField: "user_id",
            foreignField: "user_id",
            as: "userInfo"
        }, },
    { $project:{
            "userInfo.user_id":1,
            "userInfo.nickname":1,
            "userInfo.avatar":1,
            "userInfo.sex":1, } },
]
//根据商品编号联合查询商品集合中的对应信息
let opt3 = [
    {$match:{"Art_No":artNo}},
    { $lookup:{
            from: "users",
            localField: "evaluation.user_id",
            foreignField: "user_id",
            as: "comment" }, },
    {$unwind:'$comment'},
    { $project:{
            "comments":"$evaluation.comments",
            "commentImg":"$evaluation.commentImg",
            "user_id":"$comment.user_id",
            "nickname":"$comment.nickname",
            "avatar":"$comment.avatar",
            "sex":"$comment.sex", } },
]
//根据商品信息集合的ISBN查询图书集合中对应ISBN的图书信息
Commodity.aggregate(opt, (err, data) => {
    if (err) {
       res.status(500).send({errCode:"1",msg:"服务器内部错误"})
    } else {
        var map = {}
        Object.assign(map,data[0]);
        Object.assign(map,data[0].bookInfo[0])
        delete map.bookInfo;
        //根据用户ID联合查询用户信息集合中的信息
        Commodity.aggregate(opt2, (err,data) => {
            if(err){
                res.status(500).send({errCode:"1",msg:"服务器内部错误"})
            }else{
                Object.assign(map, data[0].userInfo[0])
                //根据商品编号联合查询商品集合中的对应信息
                Commodity.aggregate(opt3,(err,data)=>{
                    if(err){
                        res.status(500).send({errCode:"1",msg:"服务器内部错误"})
                    }else{
                        for(let i=0;i<data.length;i++){
                            let comm=data[i].comments[i];
                            data[i].comments = comm;
                            data[i].commentImg = data[i].commentImg[i];
                       }
                        map.evaluation = data
                        //返回数据
                        res.send({errorCode:"0",msg:"success",data:{commodity:map}});
                    }
                })
            }
        })
    }
});
}